﻿using FreeSql;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using TomNet.Entity;

namespace TomNet.FreeSql.Base
{
    /// <summary>
    /// 仓储模式基类
    /// </summary>
    public abstract class RepositoryBase<TEntity, TKey> : BaseRepository<TEntity, TKey> where TEntity : class, new()
    {
        private readonly IServiceProvider _provider;

        protected RepositoryBase(UnitOfWorkManager uowm, IServiceProvider provider) : base(uowm.Orm, null, null)
        {
            uowm.Binding(this);
            _provider = provider;// 有它就有梦想
            var user = _provider.GetCurrentUser()?.Identity;
        }

        #region 软删除
        public int SoftDelete(TKey id)
        {
            if (typeof(ISoftDeletable).IsAssignableFrom(typeof(TEntity)))
            {
                return UpdateDiy
                        .SetDto(new
                        {
                            DeletedTime = DateTime.Now,
                        })
                        .WhereDynamic(id)
                        .ExecuteAffrows();
            }
            return 0;

        }

        public async Task<int> SoftDeleteAsync(TKey id)
        {
            if (typeof(ISoftDeletable).IsAssignableFrom(typeof(TEntity)))
            {
                return await UpdateDiy
                      .SetDto(new
                      {
                          DeletedTime = DateTime.Now,
                      })
                      .WhereDynamic(id)
                      .ExecuteAffrowsAsync();
            }
            return 0;
        }
        #endregion

        #region 重写新增
        //public override TEntity Insert(TEntity entity)
        //{
        //    return base.Insert(entity);
        //}

        //public override Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
        //{
        //    return base.InsertAsync(entity, cancellationToken);
        //}

        //public override List<TEntity> Insert(IEnumerable<TEntity> entitys)
        //{
        //    return base.Insert(entitys);
        //}

        //public override Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
        //{
        //    return base.InsertAsync(entitys, cancellationToken);
        //}
        #endregion

        #region 重写修改

        //public override int Update(TEntity entity)
        //{
        //    return base.Update(entity);
        //}

        //public override Task<int> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
        //{
        //    return base.UpdateAsync(entity, cancellationToken);
        //}

        //public override int Update(IEnumerable<TEntity> entitys)
        //{
        //    return base.Update(entitys);
        //}

        //public override Task<int> UpdateAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
        //{
        //    return base.UpdateAsync(entitys, cancellationToken);
        //}

        //public int Update(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression)
        //{
        //    return UpdateDiy
        //        .Set(updateExpression)            
        //        .Where(predicate).ExecuteAffrows();
        //}

        //public async Task<int> UpdateAsync(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression)
        //{
        //    return await UpdateDiy
        //        .Set(updateExpression)
        //        .Where(predicate)
        //        .ExecuteAffrowsAsync();
        //}

        #endregion
    }

    /// <summary>
    /// 以long为主键类型的仓储模式基类
    /// </summary>
    public abstract class RepositoryLongBase<TEntity> : RepositoryBase<TEntity, long> where TEntity : class, new()
    {
        protected RepositoryLongBase(UnitOfWorkManager uowm, IServiceProvider provider) : base(uowm, provider)
        { }
    }

    /// <summary>
    /// 以Guid为主键类型的仓储模式基类
    /// </summary>
    public abstract class RepositoryGuidBase<TEntity> : RepositoryBase<TEntity, Guid> where TEntity : class, new()
    {
        protected RepositoryGuidBase(UnitOfWorkManager uowm, IServiceProvider provider) : base(uowm, provider)
        { }
    }
}
